<template>
  <div class="xtx-cart-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>购物车</XtxBreadItem>
      </XtxBread>
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <XtxCheckbox
                  :modelValue="isAllEffectSelected"
                  @change="effectCheckAll"
                  >全选</XtxCheckbox
                >
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="goods in effectiveList" :key="goods.id">
              <!-- v-model语法糖 ==》 ：modelValue  vuex中的数据不能直接双向绑定的 -->
              <!-- 该组件提供了change事件，可以用来监听数据变化 -->
              <td>
                <XtxCheckbox
                  :modelValue="goods.selected"
                  @change="(selected) => checkChange(selected, goods)"
                />
              </td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/goods/${goods.id}`"
                    ><img :src="goods.picture" alt=""
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">{{ goods.name }}</p>
                    <p class="attr">{{ goods.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ goods.nowPrice }}</p>
                <p v-if="goods.price > goods.nowPrice">
                  比加入时降价
                  <span class="red"
                    >&yen;{{ (goods.price - goods.nowPrice).toFixed(2) }}</span
                  >
                </p>
              </td>
              <td class="tc">
                <XtxNumbox
                  :isLabel="false"
                  :min="1"
                  :max="goods.stock"
                  :modelValue="goods.count"
                  @change="(num) => changeCount(num, goods)"
                />
              </td>
              <td class="tc">
                <p class="f16 red">
                  &yen;{{ (goods.nowPrice * goods.count).toFixed(2) }}
                </p>
              </td>
              <td class="tc">
                <p>
                  <a class="green" href="javascript:;" @click="delCart(goods)"
                    >删除</a
                  >
                </p>
              </td>
            </tr>
            <tr v-if="effectiveList.length <= 0">
              <td colspan="6">
                <div class="cart-none" style="text-align: center">
                  <img src="~@/assets/images/none.png" alt="" />
                  <p>购物车内暂时没有商品</p>
                  <div class="btn" style="margin: 20px">
                    <XtxButton type="primary" @click="$router.push('/')"
                      >继续逛逛</XtxButton
                    >
                  </div>
                </div>
              </td>
            </tr>
          </tbody>
          <!-- 无效商品 -->
          <tbody v-if="ineffectiveList.length">
            <tr>
              <td colspan="6"><h3 class="tit">失效商品</h3></td>
            </tr>
            <tr v-for="goods in ineffectiveList" :key="goods.skuId">
              <td></td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/goods/${goods.id}`"
                    ><img :src="goods.picture" alt=""
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">{{ goods.name }}</p>
                    <p class="attr">{{ goods.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ goods.nowPrice }}</p>
              </td>
              <td class="tc">{{ goods.count }}</td>
              <td class="tc">
                <p>&yen;{{ (goods.nowPrice * goods.count).toFixed(2) }}</p>
              </td>
              <td class="tc">
                <p>
                  <a class="green" href="javascript:;">删除</a>
                </p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <!-- SelectedList,
      selectedAllCounts,
      selectedAllPrice -->
      <div class="action">
        <div class="batch"></div>
        <div class="total">
          共 {{ effectiveList.length }} 项有效商品，已选择
          {{ SelectedList.length }} 项，共计{{
            selectedAllCounts
          }}件，商品合计：
          <span class="red">¥{{ selectedAllPrice }}</span>
          <XtxButton type="primary" @click="goSettled">下单结算</XtxButton>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { message } from '@/components/Message/index'
import { useRouter } from 'vue-router'
export default {
  name: 'XtxCartPage',
  setup () {
    const store = useStore()
    const effectiveList = computed(() => store.getters['cart/effectiveList'])
    const ineffectiveList = computed(() => store.getters['cart/ineffectiveList'])
    const isAllEffectSelected = computed(() => store.getters['cart/isAllEffectSelected'])
    const SelectedList = computed(() => store.getters['cart/SelectedList'])
    const selectedAllCounts = computed(() => store.getters['cart/selectedAllCounts'])
    const selectedAllPrice = computed(() => store.getters['cart/selectedAllPrice'])

    // 点击单选获取复选框的状态
    const checkChange = (selected, goods) => {
      // console.log(selected, goods)
      store.dispatch('cart/singCheckAPI', { selected, goods })
      // console.log(isAllEffectSelected)
    }
    // 全选
    const effectCheckAll = (selected) => {
      store.dispatch('cart/effectCheckAllAPI', selected)
    }
    // 删除
    const delCart = (goods) => {
      store.dispatch('cart/deleteCartAPI', goods.skuId)
    }
    // 改变数量
    const changeCount = (num, goods) => {
      // 需要根据num，修改goods里的count，才会改变
      console.log(goods)
      store.dispatch('cart/changeCountAPI', { num, goods })
    }
    // 下单结算
    // 跳转结算页面
    const router = useRouter()

    // 下单
    const goSettled = () => {
      // 1.不能为非登录状态
      if (!store.state.user.profile.token) {
        message({ type: 'warn', text: '请先登录' })
        return false
      }
      // 2. 选择的商品数量不能为零
      if (selectedAllCounts.value === 0) {
        message({ type: 'warn', text: '请选择商品下单' })
        return false
      }
      // 跳转到订单结算页
      router.push('/settlement')
    }

    return {
      effectiveList,
      ineffectiveList,
      checkChange,
      isAllEffectSelected,
      effectCheckAll,
      SelectedList,
      selectedAllCounts,
      selectedAllPrice,
      delCart,
      changeCount,
      goSettled
    }
  }
}
</script>
<style scoped lang="less">
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: @priceColor;
}
.green {
  color: @xtxColor;
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .xtx-checkbox {
    color: #999;
  }
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}
.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
